package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.exception.PlatformBizErrorCodes;
import com.iwomy.secureplat.common.core.util.MsgUtils;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicPlaceFloorDTO;
import com.iwomy.secureplat.platform.entity.BasicInstallLocationEntity;
import com.iwomy.secureplat.platform.entity.BasicPlaceFloorEntity;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.service.BasicInstallLocationService;
import com.iwomy.secureplat.platform.service.BasicPlaceFloorService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 楼层管理
 *
 * @author pig
 * @date 2023-11-30 13:55:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicPlaceFloor")
@Tag(description = "basicPlaceFloor", name = "楼层管理管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicPlaceFloorController {

	private final BasicPlaceFloorService basicPlaceFloorService;

	private final BasicInstallLocationService basicInstallLocationService;

	/**
	 * 分页查询
	 *
	 * @param page            分页对象
	 * @param basicPlaceFloorDTO 楼层管理
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceFloor_view')")
	public R getBasicPlaceFloorPage(@ParameterObject Page page, @ParameterObject BasicPlaceFloorDTO basicPlaceFloorDTO) {
		return R.ok(basicPlaceFloorService.getBasicPlaceFloorPage(page, basicPlaceFloorDTO));
	}

	/**
	 * 分页查询
	 * @param basicPlaceFloorDTO 楼层管理
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceFloor_view')")
	public R getBasicPlaceFloorPages(@ParameterObject BasicPlaceFloorDTO basicPlaceFloorDTO) {
		return R.ok(basicPlaceFloorService.getBasicPlaceFloorList( basicPlaceFloorDTO));
	}

	/**
	 * 通过楼层id查询绑定安装点列表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询" , description = "通过id查询" )
	@GetMapping("/getFloorInstallList/{id}" )
	public R getFloorInstallList(@ParameterObject Page page,@PathVariable("id" ) Long id) {
		return R.ok(basicInstallLocationService.getFloorInstallList(page,id));
	}

	/**
	 * 通过id查询楼层管理
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceFloor_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicPlaceFloorService.getById(id));
	}

	/**
	 * 新增楼层管理
	 *
	 * @param basicPlaceFloor 楼层管理
	 * @return R
	 */
	@Operation(summary = "新增楼层管理", description = "新增楼层管理")
	@SysLog("新增楼层管理")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceFloor_add')")
	public R save(@RequestBody BasicPlaceFloorEntity basicPlaceFloor) {
		BasicPlaceFloorEntity wrapperEntity = new BasicPlaceFloorEntity();
		wrapperEntity.setFloorName(basicPlaceFloor.getFloorName());
		wrapperEntity.setBuildingId(basicPlaceFloor.getBuildingId());
		LambdaQueryWrapper<BasicPlaceFloorEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicPlaceFloorEntity one = basicPlaceFloorService.getOne(wrapper);
		if (ObjectUtil.isNotEmpty(one)) {
//			throw new PlatformException("楼层已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.PLACEFLOOR_EXIST));
		}
		basicPlaceFloor.setDeptId(SecurityUtils.getUser().getDeptId());
		return R.ok(basicPlaceFloorService.save(basicPlaceFloor));
	}

	/**
	 * 修改楼层管理
	 *
	 * @param basicPlaceFloor 楼层管理
	 * @return R
	 */
	@Operation(summary = "修改楼层管理", description = "修改楼层管理")
	@SysLog("修改楼层管理")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceFloor_edit')")
	public R updateById(@RequestBody BasicPlaceFloorEntity basicPlaceFloor) {
		BasicPlaceFloorEntity wrapperEntity = new BasicPlaceFloorEntity();
		wrapperEntity.setFloorName(basicPlaceFloor.getFloorName());
		wrapperEntity.setBuildingId(basicPlaceFloor.getBuildingId());
		LambdaQueryWrapper<BasicPlaceFloorEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicPlaceFloorEntity one = basicPlaceFloorService.getOne(wrapper);
		if (ObjectUtil.isNotEmpty(one)&&!one.getId().equals(basicPlaceFloor.getId())) {
//			throw new PlatformException("楼层已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.PLACEFLOOR_USED_MODIFY));
		}
		return R.ok(basicPlaceFloorService.updateById(basicPlaceFloor));
	}

	/**
	 * 通过id删除楼层管理
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除楼层管理", description = "通过id删除楼层管理")
	@SysLog("通过id删除楼层管理")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceFloor_del')")
	public R removeById(@RequestBody Long[] ids) {
		long count = basicInstallLocationService.count(Wrappers.<BasicInstallLocationEntity>lambdaQuery()
				.in(BasicInstallLocationEntity::getFloorId, ids));
		if (count > 0) {
			return R.failed("已绑定安装点无法删除，请先解除绑定");
		}
		return R.ok(basicPlaceFloorService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicPlaceFloor 查询条件
	 * @param ids             导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicPlaceFloor_export')")
	public List<BasicPlaceFloorEntity> export(BasicPlaceFloorEntity basicPlaceFloor, Long[] ids) {
		return basicPlaceFloorService.list(Wrappers.lambdaQuery(basicPlaceFloor).in(ArrayUtil.isNotEmpty(ids), BasicPlaceFloorEntity::getId, ids));
	}
}